package com.wanxi.tcp.demo1客户端发送一次数据服务器;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * TCP 客户端,演示客户端跟服务器端说话，说一次服务器就停机了
 */
public class TCPClient {
    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动....");
        //1 创建客户端socket 连接谁，哪个端口
        Socket socket = new Socket("127.0.0.1", 30000);
        //2 准备发送的信息
        String message = "中大夫撒";
        //3 发送信息
        //3.1 首先获取输出流
//        OutputStream outputStream = socket.getOutputStream();
//        outputStream.write(message.getBytes(StandardCharsets.UTF_8));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(message);
        writer.newLine();
        writer.flush();
        System.out.println("客户端消息发送完毕....");
        //4 关闭流
//        outputStream.close();
        writer.close();
        socket.close();
    }
}
